/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.view.component.composition;

import br.com.loja.view.component.factory.ViewHadler;
import br.com.loja.view.component.factory.EditorToMany;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author DavidLuiz
 */
public class CommonToMany<E> extends JList<E> implements ViewHadler<List<E>>, EditorToMany {

    private final Class<E> clazz;

    public CommonToMany(Class<E> clazz) {
        this.clazz = clazz;
    }

    @Override
    public void setValueModel(List<E> value) {
        DefaultListModel<E> listModel = new DefaultListModel<>();

        for (E element : value) {
            listModel.addElement(element);
        }

        setModel(listModel);
        updateUI();
    }

    @Override
    public List<E> getValueModel() {
        List<E> list = new ArrayList<>();
        DefaultListModel<E> listModel = (DefaultListModel<E>) getModel();
        for (int i = 0; i < listModel.size(); i++) {
            list.add(listModel.get(i));
        }
        return list;
    }

    @Override
    public Component getEditorToMany() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
